package initialize

import (
	"common/global"
	"common/model"
	"fmt"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"gorm.io/gorm/logger"
)

func InitMysql() {
	cf := Config.Mysql
	var err error
	dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=True&loc=Local", cf.User, cf.Password, cf.Host, cf.Port, cf.Database)
	global.Db, err = gorm.Open(mysql.Open(dsn), &gorm.Config{
		Logger: logger.Default.LogMode(logger.Error),
	})
	if err != nil {
		panic(err)
	}
	err = global.Db.AutoMigrate(&model.User{}, &model.SignRecord{}, &model.LuckyDrawRecord{})
	if err != nil {
		panic("迁移失败")
	}
	fmt.Println(" init mysql  success")

}
